package dt.inlee.common.encoder;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;

public class TestInport {
	
	public static void excelWithTitleFoot(HttpServletResponse response,String head,
			String[] name,List<?> val,String cycle,int[] cols,String[] totleNum) throws Exception {

		String title = new String(head.getBytes(), "ISO8859-1");
//				+ UtilDataTime.getFormatDate(UtilDataTime.nowDate());
		response.setContentType("application/vnd.ms-excel;charset=GBK");
		response.addHeader("Content-Disposition", "attachment; filename=\""
				+ title + ".xls\"");
		jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(response
				.getOutputStream());
		
		int j = val.size() / 40000 + 1;
		for (int i = 0; i < j; i++) {
			jxl.write.WritableSheet ws = wwb.createSheet(head + i, i);
			
			//自定义大表头
			jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.ARIAL, 
					15, WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);
			jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc);
			wcfFC.setAlignment(jxl.format.Alignment.CENTRE);
			wcfFC.setBorder(Border.ALL, BorderLineStyle.THIN);
			ws.mergeCells(0,0,name.length-1,0);
			//添加大表头
			ws.addCell(new Label(0,0,cycle,wcfFC));	
			WritableCellFormat cellFormat = new WritableCellFormat();
			cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
			//设置小表头
			for(int w=0;w<name.length;w++){
				ws.addCell(new Label(w, 1, name[w],cellFormat));
			}
			//设置表体
			for (int k = 40000 * i; k < 40000 * (i + 1) && k < val.size(); k++) {
				String[] va = (String[]) val.get(k);
				for(int m=0;m<va.length;m++){ 
					ws.addCell(new Label(m, (k -40000*i)+ 2, va[m],cellFormat));
				}
			}
			WritableCellFormat downFomart = new WritableCellFormat ();
			downFomart.setBorder(Border.NONE, BorderLineStyle.THIN);
			ws.addCell(new Label(0,val.size()+3,"合计：",downFomart));
			
			for(int t=0; t<cols.length; t++){
				ws.addCell(new Label(cols[t],val.size()+3,totleNum[t],downFomart));			
			}
			ws.addCell(new Label(1,val.size()+4,"制表人：",downFomart));
			ws.addCell(new Label(4,val.size()+4,"稽核人：",downFomart));
			ws.addCell(new Label(7,val.size()+4,"部门负责人：",downFomart));
		}
		wwb.write();
		wwb.close();
	}
	
	public static void main(String[] args) {

	}

}
